package main

import (
	"fmt"
	"strings"
)

func main() {
	pattern := "aaa"
	s := "dog cat cat fish"
	fmt.Println(wordPattern(pattern, s))
}

func wordPattern(pattern string, s string) bool {
	m := make(map[byte]string, len(pattern))
	r := make(map[string]struct{})
	data := strings.Split(s, " ")
	l := len(pattern)
	if l != len(data) {
		return false
	}
	for i := 0; i < l; i++ {
		if v, ok := m[pattern[i]]; ok {
			if v != data[i] {
				return false
			}
		} else {
			if _, ok := r[data[i]]; ok {
				return false
			}
			m[pattern[i]] = data[i]
			r[data[i]] = struct{}{}
		}
	}

	return true
}
